﻿/*!	@file	r_alloc_new_delete.h
	@brief	new,delete定義
Copyright (c) 2009 Yuya Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef _R_ALLOC_NEW_DELETE_H_
#define _R_ALLOC_NEW_DELETE_H_

#include "r_alloc_def.h"
#include <new>
#include "r_alloc_base.h"


//! new deleteに利用するアロケータの設定
/*! この関数はシステムにより一度だけ呼ばれます
*/
void	r_alloc_setAllocator ( cRAllocBase* allocator );

//! new/delete定義
/*! このグローバルなnew/deleteはゲームシステムでは使用しないで下さい。
アロケータ割り当てが困難もしくは面倒なライブラリのアロケータを全部置き換える為に使用してください。
*/
void	*operator new ( r_size size );
void	*operator new[] ( r_size size );
void	operator delete ( void *pv );
void	operator delete[] ( void *pv );


// 各種アロケータからのnew delete の実装用マクロ
//! .hで定義
#define	RALLOC_PLACEMENT_NEW_DELETE_DECLEARE							void*	operator	new(r_size size);\
																		void	operator	delete(void *ptr);
//! .cppで定義
#define	RALLOC_PLACEMENT_NEW_DELETE_IMPLEMENT(className, allocators)	void*	className :: operator	new(r_size size)\
																														{\
																															return allocators->malloc(size);\
																														}\
																		void	className :: operator	delete(void* ptr)\
																														{\
																															allocators->free(ptr);\
																														}
//! .cppでアロケータインデックスで定義
#define	RALLOC_PLACEMENT_NEW_DELETE_IMPLEMENT_DEF(className, allocdef)	void*	className :: operator	new(r_size size)\
																														{\
																															return gRAllocDefAllocator[allocdef]->malloc(size);\
																														}\
																		void	className :: operator	delete(void* ptr)\
																														{\
																															gRAllocDefAllocator[allocdef]->free(ptr);\
																														}
//! .cppでシステムアロケータ使用で定義
#define	RALLOC_PLACEMENT_NEW_DELETE_IMPLEMENT_SYSTEM(className)			void*	className :: operator	new(r_size size)\
																														{\
																															cRAllocSystem::instance allocators;\
																															return allocators->malloc(size);\
																														}\
																		void	className :: operator	delete(void* ptr)\
																														{\
																															cRAllocSystem::instance allocators;\
																															allocators->free(ptr);\
																														}

//! .hで全部定義
#define	RALLOC_PLACEMENT_NEW_DELETE(allocators)							void*	operator	new(r_size size){return allocators->malloc(size);}\
																		void	operator	delete(void *ptr){allocators->free(ptr);}
//! .hでアロケータインデックスで定義
#define	RALLOC_PLACEMENT_NEW_DELETE_DEF(allocdef)						void*	operator	new(r_size size){return gRAllocDefAllocator[allocdef]->malloc(size);}\
																		void	operator	delete(void *ptr){gRAllocDefAllocator[allocdef]->free(ptr);}

#endif // _R_ALLOC_NEW_DELETE_H_
